package components
{
	import Classes.pi_events;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_label;
	import UIFree.uif_component;
	
	import UIUtils.uif_util;
	
	import editor.*;
	
	import flash.events.Event;
	import flash.geom.Rectangle;


	public class pj_albums extends pj_tabbox {
		protected var _albums		: Array;
		protected var _userInfo		: pj_userInfo;
		protected var _uid			: String;
		protected var _label		: uif_label;
		protected var _button		: pj_button;
		
		public function pj_albums(parent:uif_component, list:Array, ax:int=0, ay:int=0, awidth:int=0, aheight:int=0, styleClass:String='', countInfo:Boolean=true) {
			super(parent, list, ax, ay, awidth, aheight, awidth - 150, styleClass, countInfo);
			_uid		= pj.externalserver.viewer_id;
			_userInfo 	= new pj_userInfo(this);
		}
		
		protected function assignData(data: Array, itemClass: Class): void {
			if (!data) data = [];
			else data = (new Array()).concat(data);
			var inner: Rectangle = _itemsLayer.displayRect;
			var a_width: int = _itemsLayer.calcFrameWidth(1);
			var count: int = Math.floor(_itemsLayer.displayRect.width / a_width);
			while (data.length < count) data.push(null);
			
			_itemsLayer.assignList(data, a_width, function(_scroll: uif_component, inner: Rectangle, index: int): pi_previewItem {
				if (data[index]) data[index].privacy = _albums[tabs.selectIndex].privacy;
				return new itemClass(index, _scroll, inner.x, inner.y, inner.width, inner.height, data[index], 'tabItem_unselect');
			});
		}
		
		protected function updateLabels(): void {
			if (!_label) {
				var inner: Rectangle = getInner();
				_label = (new uif_label(this, inner.x + inner.width - 150, inner.y, 150, 30, 'box_label'));
				_button = new pj_button(this, inner.x + inner.width - 150, inner.y, 150, 'Загрузить ваши фото', function(e: Event): void {
					app.dispatchEvent(new ObjectEvent(pi_events.USERALBUMS, {uid:pj.externalserver.viewer_id}));
				});
			}
			_label.visible = _uid == pj.externalserver.viewer_id;
			_button.visible = _uid != pj.externalserver.viewer_id;
			_label.text = (_albums.length > 0)?pi_data.USERPHOTOS:pi_data.NOALBUMS;
		}
		
		public function get albumList(): Array {
			return _albums;
		}
		
		public function set albumList(a_list: Array): void {
			assignList(a_list);
		}
		
		public function assignAlbums(a_uid: String, list: Array): void {
			_uid = a_uid;
			assignList(list);
		}
		
		override public function assignList(a_albums: Array): void {
			if (!_uid) _uid = pj.externalserver.viewer_id;
			_albums = a_albums;
			super.assignList(_albums);
			if (_albums.length > 1) tabs.selectIndex = 1;
			else tabs.selectIndex = 0;
			updateLabels();
		}
		
		public function get curUID(): String {
			return _uid;
		}
		
		override protected function maxWidthTab(list: Array): int {
/*			var itStyle: Object = app.styleByName(style['tabitem_class']);
			uif_util.values(list, 'smallTitle');
			var _tlist: Array = new Array();
			for each (var item: Object in list) 
				_tlist.push(item.smallTitle);*/
				
			return super.maxWidthTab(uif_util.values(list, 'smallTitle'));
		}
		
		override protected function createItem(list: Array, inner: Rectangle, index: int, styleName: String): uif_tabItem {
			inner.inflate(9, 0);
			inner.x += 9;
			var item: uif_tabItem = new uif_tabItem(tabs.scrollArea, inner, styleName);
			item.text = list[index].smallTitle;
			return item; 
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			super.doChangeSelect(tabIndex);
			pj.externalserver.getPhotos(_uid, _albums[tabIndex].aid, function(data:*): void {
				assignData(data, pi_userPhoto);
			});
		}		
	}
}